<template>
  <div class="app-container">
    <el-dialog :visible.sync="openStatus" :show="openStatus" @open="doOpen" @close="doClose">
      <div class="filter-container">
        <el-input v-model="listQuery.name" placeholder="路由名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-input v-model="listQuery.menuName" placeholder="菜单名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-input v-model="listQuery.permission" placeholder="权限标识" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" @click="handleFilter">
          <svg-icon icon-class="search" /> 搜索
        </el-button>
      </div>

      <el-table
        ref="multipleTable"
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-key="getRowKey"
        :expand-row-keys="expandRows"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @sort-change="sortChange"
        @expand-change="expandChange"
      >
        <el-table-column label="路由名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="菜单名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.menuName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="权限标识" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.permission }}</span>
          </template>
        </el-table-column>
        <el-table-column label="菜单类型" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 1">菜单</el-tag>
            <el-tag v-if="scope.row.type === 2" type="info">权限</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.isSelected === 1" type="primary" circle @click="handlePutDownMenus(scope.row)">
              <svg-icon icon-class="remove" />
            </el-button>
            <el-button v-if="scope.row.isSelected === 0" type="primary" circle @click="handlePickUpMenus(scope.row)">
              <svg-icon icon-class="add" />
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { selectMenu, pickUpRoleMenus, putDownRoleMenus } from '@/api/menu'
import waves from '@/directive/waves'

export default {
  name: 'MenuTable',
  components: { },
  directives: { waves },
  props: {
    roleId: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      listLoading: true,
      expandRows: [],
      listQuery: {
        name: '',
        menuName: '',
        permission: '',
        roleId: '',
        ids: [],
        sortable: 'asc'
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      openStatus: this.visible
    }
  },
  watch: {
    roleId(val) {
      this.listQuery.roleId = val
    },
    visible(val) {
      this.openStatus = val
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      selectMenu(this.listQuery).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    getRowKey(row) {
      return row.id
    },
    handleFilter() {
      this.getList()
    },
    doOpen() {
      this.getList()
    },
    doClose() {
      this.$emit('dialogData', false)// 子组件对openStatus修改后向父组件发送事件通知
    },
    closeDialogForm() {
      this.openStatus = false
    },
    handlePickUpMenus(row) {
      const rolePermission = { roleId: this.roleId, functionId: '' }
      rolePermission.functionId = row.id
      pickUpRoleMenus(rolePermission).then(() => {
        this.getList()
        this.$notify({
          title: 'Success',
          message: '分配菜单成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handlePutDownMenus(row) {
      const rolePermission = { roleId: this.roleId, functionId: '' }
      rolePermission.functionId = row.id
      putDownRoleMenus(rolePermission).then(() => {
        this.getList()
        this.$notify({
          title: 'Success',
          message: '删除菜单成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    expandChange(row, expanded) {
      if (expanded) {
        this.expandRows.push(row.id)
      } else {
        const closeRowId = this.expandRows.indexOf(row.id)
        this.expandRows.splice(closeRowId)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sortable = 'asc'
      } else {
        this.listQuery.sortable = 'desc'
      }
      this.handleFilter()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}`
        ? 'ascending'
        : sort === `-${key}`
          ? 'descending'
          : ''
    }
  }
}
</script>
